package com.lovo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lovo.bean.Detail;
import com.lovo.bean.SysGoods;
import com.lovo.mapper.DetailMapper;
import com.lovo.service.IDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.service.ISysGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * ????????ϸ? 服务实现类
 * </p>
 *
 * @author lovo
 * @since 2022-09-17
 */
@Service
@Transactional
public class DetailServiceImpl extends ServiceImpl<DetailMapper, Detail> implements IDetailService {


    @Autowired
    DetailMapper detailMapper;

    @Autowired
    ISysGoodsService goodsService;

    @Override
    public void sendDetail(Detail detail) {

        QueryWrapper<Detail> qw = new QueryWrapper<>();
        qw.eq("d_batch", detail.getDBatch());
        qw.eq("goodsName", detail.getGoodsname());
        qw.eq("d_code", detail.getDCode());
        if (detailMapper.selectOne(qw) == null) {//查询商品信息表有没有这个商品，没有 新添商品信息表库存
            QueryWrapper<SysGoods> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("goodsName", detail.getGoodsname());
            SysGoods sysGoods = goodsService.getBaseMapper().selectOne(queryWrapper);
            if (sysGoods == null) {//查询库存商品有没有这个商品 如果没有 新添加库存商品
                sysGoods = new SysGoods();
                sysGoods.setStocknum(detail.getNum());
                sysGoods.setGoodsname(detail.getGoodsname());
                sysGoods.setPBatch(detail.getDSize());
                sysGoods.setPCode(detail.getDCode());
                sysGoods.setPType(detail.getDType());
                sysGoods.setSellprice(detail.getDPrice());
                sysGoods.setMinnum(20);
                goodsService.getBaseMapper().insert(sysGoods);
            } else {//如果有这个商品 直接添加数量
                sysGoods.setStocknum(sysGoods.getStocknum() + detail.getNum());
                goodsService.updateById(sysGoods);
            }
            detail.setGoodsid(sysGoods.getGoodsid());
            detailMapper.insert(detail);
        }
    }

    @Override
    public List<Detail> findDetailByProductId(Integer productId) {
        QueryWrapper<Detail> qw = new QueryWrapper<>();
        qw.eq("goodsId", productId);
        return detailMapper.selectList(qw);
    }

    @Override
    public boolean sellProduct(Integer productId, Integer num) {
        SysGoods sysGoods = goodsService.getBaseMapper().selectById(productId);
        //前端调用后端 销售商品接口 查看商品是否下架 如果下架 返回false;
        if (sysGoods.getPStatus().equals("已下架")) {
            return false;
        }
        //如果商品库存数量 小于购买数量 返回 false 购买失败
        if (sysGoods.getStocknum() < num) {
            return false;
        }

        int a = 0;
        QueryWrapper<Detail> qw = new QueryWrapper<>();
        qw.eq("goodsId", productId);
        // 查询商品明细表李不同批次的商品
        List<Detail> list = detailMapper.selectList(qw);
        if (list.size() == 1) {
            if (list.get(0).getNum() < num) { //如果商品的数量不足返回false 提示库存不足
                return false;
            } else { //只有一个批次的商品 直接扣除数量
                list.get(0).setNum(list.get(0).getNum() - num);
                detailMapper.updateById(list.get(0));
            }
        } else {//有多个批次的商品 先扣除第一批次的商品
            if (list.get(0).getNum() < num) {//前一批次的商品数量不够 先扣除相应的数量 再扣除第二个商品
                a = num - list.get(0).getNum();
                detailMapper.deleteById(list.get(0).getInfoid());
                list.get(1).setNum(list.get(1).getNum() - a);
                detailMapper.updateById(list.get(1));
            } else {//前一批次的商品数量足够 直接扣除
                list.get(0).setNum(list.get(0).getNum() - num);
                detailMapper.updateById(list.get(0));
            }
        }
        //修改库存商品数量
        goodsService.sellGoods(productId, num);
        return true;
    }
}
